//
//  LSZPromotionalGraphicDetailsVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/10/27.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZPromotionalGraphicDetailsVC.h"
#import <JavaScriptCore/JavaScriptCore.h>
//#import "LCViewController+LCBaiduMobAdFirst.h"
#import "LCViewController+LCBaiduMobAdPrerollNative.h"
// 判断是否是iPhone X
#define iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
// 状态栏高度
#define STATUS_BAR_HEIGHT (iPhoneX ? 44.f : 20.f)
// 导航栏高度
#define NAVIGATION_BAR_HEIGHT (iPhoneX ? 88.f : 64.f)

@interface LSZPromotionalGraphicDetailsVC ()

@end

@implementation LSZPromotionalGraphicDetailsVC
-(void)clickButReturn {
    _webView.delegate = nil;
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)loadView {
    [super loadView];
    self.url = @"http://fuyin.app.inping.com/movie/play.php?id=9&usercode=";
    [self loadPrerollNative];
}
- (void)dealloc {
    NSLog(@"__func__dealloc = %s", __func__);
    [self didAdClose];
}
- (void)setupNV {
    UIView * viewTop = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, NAVIGATION_BAR_HEIGHT)];
    viewTop.backgroundColor = RGBColor(224,106,103);
    viewTop.userInteractionEnabled = YES;
    [self.view addSubview:viewTop];
    
    self.labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(125, NAVIGATION_BAR_HEIGHT-35, [[UIScreen mainScreen] bounds].size.width-250, 20)];
    self.labelTitle.textColor = [UIColor whiteColor];
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    [viewTop addSubview:self.labelTitle];
    self.labelTitle.font = [UIFont systemFontOfSize:20.0];
    
    
    UIButton * but = [[UIButton alloc]init];
    but.frame = CGRectMake(0, 0, 100, NAVIGATION_BAR_HEIGHT);
    [but addTarget:self action:@selector(clickButReturn) forControlEvents:UIControlEventTouchUpInside];
    [viewTop addSubview:but];
    
    [self setupRightBtn:viewTop];

    
    UIImageView * imageViewReturn = [[UIImageView alloc]initWithFrame:CGRectMake(20, but.frame.size.height-35, 11, 20)];
    imageViewReturn.image = [UIImage imageNamed:@"home_nav_button_back"];
    [viewTop addSubview:imageViewReturn];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.value = 0;
    [self setupNV];
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT , [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-NAVIGATION_BAR_HEIGHT)];
    _webView.delegate = self;
//    _url = @"http://fuyin.app.inping.com/holiday/index.php?usercode=qktqu1jemv3litn5xd3m?1111";
     NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",_url]];//goURLStrings[index]]];//创建URLse
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
  
    [self JavaScriptGetOc];
}

///创建右侧分享按钮
- (void)setupRightBtn:(UIView *)viewTop {
    UIButton *rightBut = [[UIButton alloc]init];
    [rightBut setTitle:@"分享" forState:UIControlStateNormal];
    rightBut.bounds = CGRectMake(0, 0, 100, NAVIGATION_BAR_HEIGHT);
    [rightBut addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [viewTop addSubview:rightBut];
    [rightBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(viewTop).offset(-16);
        make.bottom.equalTo(viewTop).offset(-9);
    }];
}
-(void)rightBtnAction{
    @try {
//        [self shareShengJingMessageWithImageArray:nil url:self.url title:self.labelTitle.text contentMessage:@"九酷福音" type:0];
    } @catch (NSException *exception) {
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.labelTitle.text = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    if ([self.labelTitle.text isEqualToString:@"福音视频"]){
//        [self startAdViewWithHeightScale:0.15 adUnitTag:@"5840732"];
    }
}
- (void)JavaScriptGetOc{
     __weak typeof(self) weakSelf = self;
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
      //clickVideo
    //点击播放视频
    context[@"clickVideo"] = ^() {
        NSArray *args = [JSContext currentArguments];
        dispatch_async(dispatch_get_main_queue(), ^{
                     //这里记录点击视频的方法
                weakSelf.value = 1;
//                if (_LCPlayer.kstatus == LCMusicPlayerStatusPlaying ){
//                    [_LCPlayer pause];//暂停播放
//                    weakSelf.PlayAudio = 1;
//                    _LCPlayer.comeFrome = 1;
//                }
            for (JSValue *jsVal in args) {
                NSLog(@"%@", jsVal.toString);
            }
            //添加视频播放历史纪录
            #pragma mark -  添加视频播放历史纪录  -
            if (weakSelf.labelTitle.text) {
                NSDictionary *json = @{@"title":weakSelf.labelTitle.text,
                                       @"link":weakSelf.url
                                       };
//                NSLog(@"历史纪录.selectedVideoData = %@<><>%@", _LCPlayer.selectedVideoData,json);
//                [_LCPlayer.selectedVideoData addObject:json];
//                [[NSUserDefaults standardUserDefaults] setObject:_LCPlayer.selectedVideoData.copy forKey:@"TENVIDEOS"];
//                NSLog(@"_LCPlayer.selectedVideoData = %@", _LCPlayer.selectedVideoData);

            }else{
                NSDictionary *json = @{@"title":@"福音视频",
                                       @"link":weakSelf.url
                                       };
//                NSLog(@"历史纪录_LCPlayer.selectedVideoData = %@<><>%@", _LCPlayer.selectedVideoData,json);
//                [_LCPlayer.selectedVideoData addObject:json];
//                [[NSUserDefaults standardUserDefaults] setObject:_LCPlayer.selectedVideoData.copy forKey:@"TENVIDEOS"];
//                NSLog(@"_LCPlayer.selectedVideoData = %@", _LCPlayer.selectedVideoData);

            }
        });

    };
    //点击分享
    context[@"share"] = ^() {
        @try {
            NSArray *args = [JSContext currentArguments];
            dispatch_async(dispatch_get_main_queue(), ^{

                for (JSValue *jsVal in args) {
                    NSLog(@"%@", jsVal.toString);
                }
                NSString * imageStr = [NSString stringWithFormat:@"%@",args[3]];
                imageStr = [imageStr stringByReplacingOccurrencesOfString:@"./" withString:@"/"];
                imageStr = [NSString stringWithFormat:@"%@%@",@"http://fuyin.app.inping.com/movie/",imageStr];
                NSArray* imageArray = @[imageStr];
                NSString * urlStr = [NSString stringWithFormat:@"%@",args[4]];
                NSString * argsOneStr = [NSString stringWithFormat:@"%@",args[1]];
                NSString * argsTwoStr = [NSString stringWithFormat:@"%@",args[2]];
                NSString * argszeroStr = [NSString stringWithFormat:@"%@",args[0]];

//                [weakSelf shareShengJingMessageWithImageArray:imageArray url:urlStr title:argsOneStr contentMessage:argsTwoStr type:[argszeroStr integerValue]];
            });
        } @catch (NSException *exception) {
        }
    };
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
//    if (self.value == 1) {
//        [_webView reload];
//
//        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
//        _LCPlayer.comeFrome = 0;
//    }
//    if (self.PlayAudio == 1) {
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [_LCPlayer play];//继续播放
//        });
//    }
    [self didAdClose];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
//    [super example1];
}

@end
